package com.example.modelfunction.shrio;

import com.example.base.Action.BaseAction;
import com.example.base.ResultVO;
import com.example.modelfunction.shrio.usermanager.mode.User;
import com.example.modelfunction.shrio.usermanager.repository.UserRepository;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.Map;

@Controller
public class LoginAction extends BaseAction{

    @Autowired
    private UserRepository userRepository;
    //跳转到主页
    @RequestMapping(value="/index")
    public String index() {
        return "index";
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping(value="/logout",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO logout(){

        try {
            //退出
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return success("退出成功");
    }

    @RequestMapping(value="/shrio",method = RequestMethod.GET)
    public String shrio(){

        return "shrio";
    }
    //跳转到登录表单页面
    @RequestMapping(value="/login")
    public String login() {
        return "static/index.html";
    }

    /**
     * ajax登录请求
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value="/ajaxLogin",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> submitLogin(String username, String password) {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {

            if("".equals(username)||null==username){
                resultMap.put("status", 500);
                resultMap.put("message", "请输入用户名");
                return resultMap;
            }
            if("".equals(password)||null==password){
                resultMap.put("status", 500);
                resultMap.put("message", "请输入密码");
                return resultMap;
            }
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            User user=userRepository.findByAccount(username);
            SecurityUtils.getSubject().login(token);
            SecurityUtils.getSubject().getSession().setAttribute("user",user);
//            SecurityUtils.getSubject().getSession().setTimeout(-1);
            resultMap.put("status", 200);
            resultMap.put("token", SecurityUtils.getSubject().getSession().getId());
            resultMap.put("user",user);
            resultMap.put("message", "登录成功");

        } catch (IncorrectCredentialsException e) {
            resultMap.put("status", 500);
            resultMap.put("message", "密码错误");
        } catch (LockedAccountException e) {
            resultMap.put("status", 500);
            resultMap.put("message", "登录失败，该用户已被冻结");
        } catch (AuthenticationException e) {
            resultMap.put("status", 500);
            resultMap.put("message", "该用户不存在");
        } catch (Exception e) {
            resultMap.put("status", 500);
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }

}
